package com.qhx.rtkcloud.tcp;


import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

public abstract class TcpExchangePair implements InitializingBean {

    private List<List<TcpConnectProperty>> units;

    public List<List<TcpConnectProperty>> getUnits() {
        return units;
    }

    public void setUnits(List<List<TcpConnectProperty>> units) {
        this.units = units;
    }

    /**
     * TODO 需要配多组 做配对
     */
//    @Value("#{${mountAndUsernameMaps}}")

//    @Value("#{${ipAndPorts}}")
//   private Map<String, String> ipAndPorts = null;
    protected abstract void setSocket(String host, int port, TcpExchangeUnit tcpExchangeUnit);

    public TcpExchangePair() {


    }

    @Override
    public void afterPropertiesSet() {
        if (units == null && units.isEmpty()) {
            return;
        }

        for (List<TcpConnectProperty> list : units) {

            if (list == null || list.isEmpty() || list.size() != 2) {
                continue;
            }
            TcpExchangeUnit tcpExchangeUnit1 = new TcpExchangeUnit(new LinkedBlockingQueue<>());
            TcpExchangeUnit tcpExchangeUnit2 = new TcpExchangeUnit(new LinkedBlockingQueue<>());
            tcpExchangeUnit1.setReadQueue(tcpExchangeUnit2.getWriteQueue());
            tcpExchangeUnit2.setReadQueue(tcpExchangeUnit1.getWriteQueue());
            tcpExchangeUnit1.setConnectFreedom(list.get(0).isConnectFreedom());
            tcpExchangeUnit2.setConnectFreedom(list.get(1).isConnectFreedom());
            tcpExchangeUnit1.setWriteFreedom(list.get(0).isWriteFreedom());
            tcpExchangeUnit2.setWriteFreedom(list.get(1).isWriteFreedom());
            tcpExchangeUnit1.setPartner(tcpExchangeUnit2);
            tcpExchangeUnit2.setPartner(tcpExchangeUnit1);
            new Thread(() -> setSocket(list.get(0).getHost(), list.get(0).getPort(), tcpExchangeUnit1), list.get(0).getHost() + ":" + list.get(0).getPort()).start();
            new Thread(() -> setSocket(list.get(1).getHost(), list.get(1).getPort(), tcpExchangeUnit2), list.get(1).getHost() + ":" + list.get(1).getPort()).start();


        }


    }


    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("82.156.68.184", 2101);


        InputStream inputStream = socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream();

        new Thread(() -> {
            while (true) {
                try {
                    outputStream.write("queryNavModel".getBytes());
                    outputStream.flush();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }


        }).start();


        byte[] bytes = new byte[10240];
        int length = 0;
        while ((length = inputStream.read(bytes)) > 0) {
            System.out.println(new String(Arrays.copyOfRange(bytes, 0, length)));
        }


    }

}
